package com.battery.system.service.battery.impl;

import java.util.List;

import com.battery.common.annotation.Excel;
import com.battery.common.utils.DateUtils;
import com.battery.system.service.battery.IAdvertService;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.battery.system.mapper.AdvertMapper;
import com.battery.system.domain.Advert;
import com.battery.common.core.text.Convert;
import tk.mybatis.mapper.entity.Example;

/**
 * 广告Service业务层处理
 *
 * @author battery
 * @date 2021-08-03
 */
@Service
public class AdvertServiceImpl extends BaseServiceImpl<Advert> implements IAdvertService {
    @Autowired
    private AdvertMapper advertMapper;

    /**
     * 查询广告
     *
     * @param id 广告ID
     * @return 广告
     */
    @Override
    public Advert selectAdvertById(Long id) {
        return advertMapper.selectAdvertById(id);
    }

    /**
     * 查询广告列表
     *
     * @param advert 广告
     * @return 广告
     */
    @Override
    public List<Advert> selectAdvertList(Advert advert) {
        return advertMapper.selectAdvertList(advert);
    }

    /**
     * 新增广告
     *
     * @param advert 广告
     * @return 结果
     */
    @Override
    public int insertAdvert(Advert advert) {
        advert.setCreateTime(DateUtils.getNowDate());
        return advertMapper.insertAdvert(advert);
    }

    /**
     * 修改广告
     *
     * @param advert 广告
     * @return 结果
     */
    @Override
    public int updateAdvert(Advert advert) {
        advert.setUpdateTime(DateUtils.getNowDate());
        return advertMapper.updateAdvert(advert);
    }

    /**
     * 删除广告对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAdvertByIds(String ids) {
        return advertMapper.deleteAdvertByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除广告信息
     *
     * @param id 广告ID
     * @return 结果
     */
    @Override
    public int deleteAdvertById(Long id) {
        return advertMapper.deleteAdvertById(id);
    }

    /**
     * 校验该场景是否已有广告
     *
     * @param useSceneType
     * @return
     */
    @Override
    public boolean checkUseScene(Integer useSceneType) {
        Example example = new Example(Advert.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("useSceneType", useSceneType);
        return advertMapper.selectCountByExample(example) > 0;
    }


    /**
     * 根据场景获取对应的广告图片
     *
     * @param useSceneType
     * @return
     */
    @Override
    public Advert selectByUseSceneType(Integer useSceneType) {
        Example example = new Example(Advert.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("useSceneType", useSceneType);
        return advertMapper.selectOneByExample(example);
    }
}
